﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DawnOfConquest
{

    class TacticalBattle
    {
        //determines the input for each player and what options to draw on menu
        public enum ControlState
        {
            selectUnit, chooseOption, move, selectTarget, endGame
        }
        private Avatar p1Avatar;
        private Avatar p2Avatar;
        private Array player1Army;
        private Array player2Army;

        private Boolean isPlayer1Turn;
        public Boolean battleOver;

        private TacMap map;

        private MouseState mouseState;
        public Boolean mouseVisible;
        private KeyboardState keyboardState;
        private KeyboardState oldKeyboardState;

        private Piece selectedUnit;
        private List<Tile> selectionTiles;

        //menu info
        private String status;
        private String terrainInfo;
        private String unitInfo;
        private Texture2D displayUnit;

        //interface
        private Texture2D background;
        private Texture2D endTurn;

        public ControlState controlState                //get and set the control state
        {
            get { return controlState; }
            protected set { controlState = value; }
        }

        public TacticalBattle(Array player1Army, Array player2Army, TacMap map, ContentManager content)
        {
            //p1Avatar = player1;
            //p2Avatar = player2;

            this.player1Army = player1Army;
            this.player2Army = player2Army;

            isPlayer1Turn = true;
            this.map = map;
            battleOver = false;

            mouseVisible = true;
            controlState = ControlState.selectUnit;

            selectedUnit = null;
            status = "Combat begins";
            selectionTiles = new List<Tile>();
            //load interface items                  TODO
            background = content.Load<Texture2D>("TerrainSprites/background");
        }

        public void draw(SpriteBatch spriteBatch, ContentManager content)           //draws the map and then each tile on the map and then the menu
        {
            //background
            Texture2D background = content.Load<Texture2D>("TerrainSprites/background");
            spriteBatch.Draw(background, new Vector2(0,0), Color.White);
            //tiles

            map.draw(spriteBatch, content);

            //menu - depends on state           TODO
            //if state= move draw green on all canMoveTo spaces
            // if state=target draw red on possible targets
            //draw scroll over unit

            if (controlState == ControlState.selectUnit)
            {
                //if click and scrolled over friendly unit set unit to selected and change state to choose option       TODO
                //if end turn is clicked then refresh all units and switch players

            }
            else if (controlState == ControlState.chooseOption)
            {
                //hide mouse        TODO
                //if keyboardState has a button pressed coressponding to the options then change state and show mouse
            }
            else if (controlState == ControlState.move)
            {
                //if click and mouse XY corresponds to valid tile then move unit and return to chooseoption         TODO
            }
            else if (controlState == ControlState.selectTarget)
            {
                //if click and tile is in target list than initiate combat between selected unit and target, return to select unit          TODO
            }


        }

        public void update()                    //gathers input, updates positions, and handles combat
        {
            //update inputs
            mouseState = Mouse.GetState();
            oldKeyboardState = keyboardState;
            keyboardState = Keyboard.GetState();

            //change scroll-over display based on mouse coordinates

            if (controlState == ControlState.selectUnit)
            {
                //if click and scrolled over friendly unit set unit to selected and change state to choose option
                //if end turn is clicked then refresh armies and switch players TODO

            }
            else if (controlState == ControlState.chooseOption)
            {
                //hide mouse
                //if keyboardState has a button pressed coressponding to the options then change state and show mouse TODO
                //space = cancel, go back to selectUnit
            }
            else if (controlState == ControlState.move)
            {
                //if click and mouse XY corresponds to valid tile then move unit and return to chooseoption TODO
                //space = cancel, go back to chooseOption
            }
            else if (controlState == ControlState.selectTarget)
            {
                //if click and tile is in target list than initiate combat between selected unit and target, show mouse, return to select unit TODO
                //space = cancel, go back to chooseOption
            }

            if (isArmyDead(player1Army))
            {
                //assign gold
                endBattle();
            }
            else if (isArmyDead(player2Army))
            {
                //assign gold
                endBattle();
            }


        }

            //method that returns the tile that a mouse is scrolled over based on mouseXY coordinates (divide by size of tile to get it)
        private Tile mouseOverTile()
        {
            int x = mouseState.X;
            int y = mouseState.Y;

            if (x < 901 && y < 901)
            {
                return map.getTile(x / 30, y / 30);
            }
            else
                return null;
        }

            //method returning a list of all tiles that the selected unit can move to 

            //method returning a list of all units within attack range of selected unit

            //method that goes through array of each army and sets hasMoved and hasAttacked to false
        private void refreshArmy()
        {
            foreach (Piece i in player1Army)
            { i.resetUnit(); }
            foreach (Piece i in player2Army)
            { i.resetUnit(); }
        }

        private Boolean isArmyDead(Array army)
        {
            foreach (Piece i in player1Army)
                if (i.getHealth() > 0)
                    return false;
            return true;
        }

        private void endBattle()
        {
            mouseVisible = true;
            battleOver = true;
        }

        public TacMap getMap()
        { return map; }
    }
}
